#include <stdio.h>
#include <libusb-1.0/libusb.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <limits.h>


typedef unsigned char u8;

struct package{
	u8 header;
	u8 kind;
	u8 length_07;
	u8 length_815;
	u8 length_1623;
	u8 length_2431;
	u8 *gprmc;
	u8 tail1;
	u8 tail2;
};

void substring(char *des,char *str,int n,int index){
	char *p = des;
	char *q = str;
	int len = strlen(str);
	*(p++) = *(q);
	if(n>len) n = len-index;
	if(index<0) index=0;
	if(index>len) return ;
	q += index;
	while(n--) *(p++) = *(q++);
	*(p++) = '\0';
	
}

void BCC(u8 *data,u8 *res){
	int len = strlen(data);
	u8 checknum = '\0';
	int i=1;
	for(i=1;i<len;i++){
		checknum = checknum ^ data[i];
	}
	
	sprintf(res,"%x\n",checknum);
	
} 
int getClockTime(){
	struct timeval tv;
        gettimeofday(&tv,NULL);
        long ms = tv.tv_usec/1000;
	return ms;
}
void lenToHex(u8 *des,int len){
	
	sprintf(des,"%x",len);
	printf("%s \n",des);
	
}



void main(){
	
	//char *GPRMC = "$GPRMC,030036.00,A,3407.52249322,N,10849.89856222,E,0.032,172.2,061221,3.3,W,A"; //27
	char *GPRMC = "$014359.00,A,3407.52414287,N,10849.89848631,E,0.000,0.0,151221,3.3,W,A,646";

	int ms = getClockTime();
	char *des;	
	FILE *fp;
	fp = fopen("t.txt","ab+");
	des = (char*)malloc(sizeof(char)*4);
	lenToHex(des,90);
	fwrite(des,sizeof(des),1,fp);
	fclose(fp);
	free(des);
	
}








